//
//  DownLoad.m
//  quickLook
//
//  Created by J on 15/8/30.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "DownLoad.h"
#import "AFAppDotNetAPIClient.h"
#import "MyMD5.h"
#import "NSFileManager+Method.h"

#import "AFHTTPRequestOperationManager.h"
@implementation DownLoad

//判断网络状况

+ (void)netState
{
    
   NSUserDefaults* userdefault= [NSUserDefaults standardUserDefaults];
    
    __block BOOL isConnection;

    //<1>初始化请求队列管理者对象
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    //<2>
    //-----------扩展---------
    //判断当前网络状态
    /*
     AFNetworkReachabilityStatusNotReachable     = 0,
     AFNetworkReachabilityStatusReachableViaWWAN = 1,
     AFNetworkReachabilityStatusReachableViaWiFi = 2,
     */
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if(status == AFNetworkReachabilityStatusReachableViaWiFi)
        {
            
            JZJLog(@"download WIFI");
            
            
            isConnection=YES;
            [userdefault setBool:isConnection forKey:@"isConnection"];

        }
        else if (status == AFNetworkReachabilityStatusReachableViaWWAN)
        {
            
            
           isConnection=YES;
            [userdefault setBool:isConnection forKey:@"isConnection"];

            
            JZJLog(@"3G/GPRS");
        }
        else
        {
           isConnection=NO;
            [userdefault setBool:isConnection forKey:@"isConnection"];
            
            JZJLog(@"无网络连接");
        }
    }];
    //开启网络监控
    [manager.reachabilityManager startMonitoring];
    //只要当前网络发生改变 就会立即调用block语句块
    
    
    
}


//联网失败  读取本地文件
+ (void)getCacheDataWithPath:(NSString*)path andUrl:(NSString*)str  andBlock:(void (^)(id, NSError *))block{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:path] && [fileManager downLoadFileName:[MyMD5 md5:str] TimeOut:600*1]) {
        //读取本地文件
        //        JZJLog(@"读取本地文件");
        NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:path];
        
        if (block) {
            block(dic,nil);
            //            return 0;
        }
    }else
    {
        block(nil,nil);

        
    
    }
    
}






//static NSString * const AFAppDotNetAPIBaseURLString = @"http://api.kuaikanmanhua.com/v1/";




//获取快看首页面数据
+ (NSURLSessionDataTask *)getQuickLookDataWithPageNumber:(NSNumber*)number withBlock:(void (^)(id, NSError *))block
{
    
    
    
    NSString *str = [NSString stringWithFormat:@"comic_lists/1?limit=20&offset=%@",number];
//    JZJLog(@"%@",str);

//    NSFileManager *fileManager = [NSFileManager defaultManager];
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
    //    JZJLog(@"%@",path);
 
    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id  JSON) {
        
     [JSON writeToFile:path atomically:YES];

        
    
        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask *  tast, NSError *  error) {
        JZJLog(@"%@",error.localizedDescription);
             
        [self getCacheDataWithPath:path andUrl:str andBlock:block];
        
      
        
       
        
        
    }];
    
    
}

//adbanners

+ (NSURLSessionDataTask *)getFinderViewBannersData:(void (^)(id, NSError *))block
{

    
    NSString *str = @"banners";
    
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
    //    JZJLog(@"%@",path);
    
    
    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask *  tast, NSDictionary*  JSON) {
        
        [JSON writeToFile:path atomically:YES];
        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask *  tast, NSError *  error) {
        
        
    
        
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];

        
        
    }];
    





}


//scrollView


//TODO:


+ (NSURLSessionDataTask *)getFinderViewSrollsDataWithUrl:(NSString*)url andBlock:(void (^)(id obj, NSError *))block
{
    
    
    NSString *str = url;
    
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
    //    JZJLog(@"%@",path);
   
    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask *  tast, NSDictionary* JSON) {
        
        [JSON writeToFile:path atomically:YES];
        
        if (block) {
            block(JSON,nil);
        }

        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        //判断是否有本地文件 如果有则读取本地数据返回
        [self getCacheDataWithPath:path andUrl:str andBlock:block];

        
        
    }];
    
    
    
    
    
    
}


+ (NSURLSessionDataTask *)getFinderNavigationBarDataSearchWithUrl:(NSString*)url andBlock:(void (^)(id obj, NSError *))block
{


    
    NSString *str = [NSString stringWithFormat:@"http://api.kuaikanmanhua.com/v1/topics/search?keyword=%@&limit=20&offset=0",url];
    
    
//    JZJLog(@"*****%@",str);
    
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];

    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        
        [JSON writeToFile:path atomically:YES];

        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];

        
        
        
    }];
    
    





}


//getToolBarLikeWithUrl



+ (NSURLSessionDataTask *)getToolBarLikeWithID:(NSNumber*)ID andBlock:(void (^)(id obj, NSError *error))block {
    
    NSDictionary *dic =nil;
    NSString* str=[NSString stringWithFormat:@"comics/%@/like",ID];
    JZJLog(@"%@",str);
    return [[AFAppDotNetAPIClient sharedClient] POST:str parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        //        NSXMLParser
        //        JZJLog(@"%@",responseObject);
        
        
        if (block) {
            block(responseObject,nil);
        }
        
        //        JZJLog(@"%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        JZJLog(@"%@",[error localizedDescription]);
    }];
    
}

//热门评论


+ (NSURLSessionDataTask*)getLatestCommentWithPageNumber:(NSInteger)number andID:(NSNumber*)ID withBlock:(void(^)(id obj, NSError* error))block
{

    
    NSString *str = [NSString stringWithFormat:@"comics/%@/comments?limit=20&offset=%zd",ID,number];
    
//    NSFileManager *fileManager = [NSFileManager defaultManager];
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];

    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        
        [JSON writeToFile:path atomically:YES];

        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];

        
        
        
    }];
    







}



//最新评论



+ (NSURLSessionDataTask*)getHotCommentWithPageNumber:(NSInteger)number andID:(NSNumber*)ID withBlock:(void(^)(id obj, NSError* error))block
{

    
    NSString *str = [NSString stringWithFormat:@"comics/%@/comments?limit=20&offset=%zd&order=score",ID,number];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
    //    JZJLog(@"%@",path);

    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        [JSON writeToFile:path atomically:YES];

        
        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];
        
    }];
    





}



//右导航栏搜索

//topics?limit=20&offset=0&tag=



+ (NSURLSessionDataTask *)getRightSearchData:(NSInteger)number andTag:(NSNumber *)tag withBlock:(void (^)(id, NSError *))block
{


    
    NSString *str = [NSString stringWithFormat:@"topics?limit=20&offset=%zd&tag=%@",number,tag];
    
//    JZJLog(@"%@",str);
    
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
    //    JZJLog(@"%@",path);

    
    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        [JSON writeToFile:path atomically:YES];

        
        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];

        
    }];
    
    

    


}





//获取 collectionViewItem点击跳转

//v1/topics/133?sort=0


//获取快看首页面数据
+ (NSURLSessionDataTask *)getThirdNavDataWithID:(NSNumber *) ID andorder:(NSInteger)order andBlock:(void (^)(id, NSError *))block
{
    
    NSString *str = [NSString stringWithFormat:@"topics/%@?sort=%zd",ID,order];

    
//    JZJLog(@"getThirdNavDataWithID::%@",str);
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
    //    JZJLog(@"%@",path);
  
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        
        [JSON writeToFile:path atomically:YES];

        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];
        
        
        
    }];
    
    
}



+ (NSURLSessionDataTask *)getScrollViewInCellDataWithID:(NSInteger) ID andBlock:(void(^)(id obj, NSError* error))block
{
    
     NSString* str=[NSString stringWithFormat:@"topics/%zd?sort=0",ID];
    
    
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
    //    JZJLog(@"%@",path);

    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        [JSON writeToFile:path atomically:YES];
        
        
        

        
        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];
        
        
    }];



}





//AdscrollView点击跳转解析

+ (NSURLSessionDataTask *)getADScrollViewSkipDataWithValue:(NSNumber*) value andBlock:(void(^)(id obj, NSError* error))block
{
    
    NSString* str=[NSString stringWithFormat:@"comics/%@",value];
    
    
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];

    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        
        [JSON writeToFile:path atomically:YES];
        
        
        

        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];
        
        
        
    }];
    
    
    
}






//findVC tableview的headerview 右边button点击跳转数据请求


+ (NSURLSessionDataTask *)getFindVCTableHeaderViewRightButtonDataWithActionParameter:(NSString*)action andoffset:(NSNumber*)offset
                                                                            andBlock:(void(^)(id obj, NSError* error))block{

    
//     @"http://api.kuaikanmanhua.com/v1/"
    
    NSString* str=[NSString stringWithFormat:@"%@?limit=20&offset=%@",action,offset];
    
    
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
    //    JZJLog(@"%@",path);

    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        
        [JSON writeToFile:path atomically:YES];
        
        
        

        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        
        
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];
        

        
        
    }];
    


}


//个人页 关注的专题
//http://api.kuaikanmanhua.com/v1/fav/topics?limit=20&offset=0

+ (NSURLSessionDataTask *)getIndividualViewControllerFavTopicsWithoffset:(NSNumber*)offset andBlock:(void(^)(id obj, NSError* error))block{


    
    //     @"http://api.kuaikanmanhua.com/v1/"
    
    NSString* str=[NSString stringWithFormat:@"fav/topics?limit=20&offset=%@",offset];
    
    
//    JZJLog(@"topisc:%@",str);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
    //    JZJLog(@"%@",path);

    
    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        
        [JSON writeToFile:path atomically:YES];
        
        
        

        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        
        
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];
        

        
        
    }];
    


}



//个人页 收藏单篇
//http://api.kuaikanmanhua.com/v1/fav/comics?limit=20&offset=0

+ (NSURLSessionDataTask *)getIndividualViewControllerFavComicsWithoffset:(NSNumber*)offset andBlock:(void(^)(id obj, NSError* error))block
{
    
    
    //     @"http://api.kuaikanmanhua.com/v1/"   fav/comics?limit=20&offset=0
    
    NSString* str=[NSString stringWithFormat:@"fav/comics?limit=20&offset=%@",offset];
    
    
//    JZJLog(@"comics:%@",str);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
    //    JZJLog(@"%@",path);
 
    
    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        
        [JSON writeToFile:path atomically:YES];
        
        
        

        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        
        
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];
        

        
        
    }];
    
    



}



//快看左侧关注
//http://api.kuaikanmanhua.com/v1/fav/timeline?since=0

+ (NSURLSessionDataTask *)getQuickLookFavItemWithsince:(NSNumber*)since andBlock:(void(^)(id obj, NSError* error))block
{

    
    //     @"http://api.kuaikanmanhua.com/v1/"timeline:fav/timeline?since=0
    
    NSString* str=[NSString stringWithFormat:@"fav/timeline?since=%@",since];
    
    
//    JZJLog(@"timeline:%@",str);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];
 
    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        
        [JSON writeToFile:path atomically:YES];
        

        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];
        
        
        
        
        
    }];
    
    



}






//最新评论发送
//http://api.kuaikanmanhua.com/v1/comics/5171/comments   content     %E5%8F%8D%E5%8F%8D%E5%A4%8D%E5%A4%8D



    
    + (NSURLSessionDataTask *)sendLatestCommentWithID:(NSNumber*)ID andContent:(NSString*)content andBlock:(void(^)(id obj, NSError* error))block{
        
        
       
        NSDictionary *dic = @{@"content":content};
        
        NSString* str=[NSString stringWithFormat:@"comics/%@/comments",ID];
        return [[AFAppDotNetAPIClient sharedClient] POST:str parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
            //        NSXMLParser
//            JZJLog(@"%@",responseObject);
            
            
            if(responseObject)
            {
            
                block(responseObject,nil);
            }
            
            //        JZJLog(@"%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
            JZJLog(@"%@",[error localizedDescription]);
        }];
        
    }

 

+ (NSURLSessionDataTask *)getIntroDataWithID:(NSNumber*)ID andBlock:(void(^)(id obj, NSError* error))block
{
    
    //     @"http://api.kuaikanmanhua.com/v1/"users/311
    
    NSString* str=[NSString stringWithFormat:@"users/%@",ID];
    
    
//    JZJLog(@"users:%@",str);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //记录路径
    NSString *path  = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[MyMD5 md5:str]];

    
    
    
    return [[AFAppDotNetAPIClient sharedClient]GET:str parameters:nil success:^(NSURLSessionDataTask * __nonnull tast, id __nonnull JSON) {
        [JSON writeToFile:path atomically:YES];

        
        if (block) {
            block(JSON,nil);
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * __nonnull tast, NSError * __nonnull error) {
        
        [self getCacheDataWithPath:path andUrl:str andBlock:block];
        
        
        
        
    }];
    
    

}


//首页点赞


+ (NSURLSessionDataTask *)getWetherDataWithID:(NSNumber*)ID andBlock:(void (^)(id obj, NSError *error))block {
    

//    http://api.kuaikanmanhua.com/v1/comics/5396/like
//    http://api.kuaikanmanhua.com/v1/
    
    NSString* str=[NSString stringWithFormat:@"topics/%@/fav",ID];
    JZJLog(@"%@",str);

    
    return [[AFAppDotNetAPIClient sharedClient] POST:str parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        
        
        if (block) {
            block(responseObject,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        JZJLog(@"%@",[error localizedDescription]);
    }];

    
}













//已验证成功使用
//post收藏 topic

+ (NSURLSessionDataTask *)postDataWithID:(NSNumber*)ID andBlock:(void (^)(id obj, NSError *error))block {
    
    NSString* str=[NSString stringWithFormat:@"topics/%@/fav",ID];
    
    JZJLog(@"%@",str);

    
    return [[AFAppDotNetAPIClient sharedClient] POST:str parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        
        if (block) {
            block(responseObject,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        JZJLog(@"%@",[error localizedDescription]);
    }];
    
}




//delete请求


+ (NSURLSessionDataTask *)delteDataWithID:(NSNumber*)ID andBlock:(void (^)(id obj, NSError *error))block {
    
    NSString* str=[NSString stringWithFormat:@"topics/%@/fav",ID];
    
    
    
    return [[AFAppDotNetAPIClient sharedClient]DELETE:str parameters:nil success:^ (NSURLSessionDataTask * task, id responseObject ) {
        
        JZJLog(@"%@",responseObject);
        
        
        if (block) {
            block(responseObject,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task , NSError *error ) {
        
        
        
        
    }];
}







//关注专题
//个人页单篇
//delete请求


+ (NSURLSessionDataTask *)delteComicsWithID:(NSNumber*)ID andBlock:(void (^)(id obj, NSError *error))block {
    
    NSString* str=[NSString stringWithFormat:@"topics/%@/fav",ID];
    JZJLog(@"%@",str);
   
    
    return [[AFAppDotNetAPIClient sharedClient]DELETE:str parameters:nil success:^ (NSURLSessionDataTask * task, id responseObject ) {
        
        JZJLog(@"%@",responseObject);
        
        
        if (block) {
            block(responseObject,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task , NSError *error ) {
        
        
        
        
    }];
}


//昵称修改
+ (NSURLSessionDataTask *)changeNickNameandBlock:(void(^)(id obj, NSError* error))block
{

    NSString* str=@"account/update";
    
    
    
    return [[AFAppDotNetAPIClient sharedClient] POST:str parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        
        if (block) {
            block(responseObject,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        JZJLog(@"%@",[error localizedDescription]);
    }];


}





@end
